﻿using Common;
using log4net.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MVVM;
using Org.BouncyCastle.Crypto.Prng;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json.Nodes;
using System.Windows;

namespace GPU.Compute.Demo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public App()
        {
            AppContext.SetSwitch("Switch.System.Windows.Media.EnableHardwareAccelerationInRdp", true);//开启RDP
        }

        /// <summary>
        /// Gets the current <see cref="App"/> instance in use
        /// </summary>
        public new static App Current => (App)Application.Current;

        /// <summary>
        /// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
        /// </summary>
        public IServiceProvider Services { get; private set; } = ConfigureServices();

        /// <summary>
        /// Configures the services for the application.
        /// </summary>
        private static IServiceProvider ConfigureServices()
        {
            return new ServiceCollection().AutoInjection().BuildServiceProvider();
        }

        /// <summary>
        /// 程序启动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            var aaa = new
            {
                bbb = "test",
                ccc = true,
                ddd = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14 },
                xxx = 123
            };

            var data = aaa.ToJsonString().ToDynamic();
            if (data.xxx > 100)
            {
                var dd = data.ddd;
            }

            //设置当前程序集
            this.ShutdownMode = ShutdownMode.OnMainWindowClose;

            //注册EncodingProvider实现对中文编码的支持
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            //解决绑定浮点类型不能输入浮点数问题
            FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;

            //注册全局异常处理
            this.RegistUnhandledExceptionHandle();

            //创建主程序并启动
            var MainWindow = App.Current.Services.GetService<MainWindow>();//泛型类必须为主窗口类，不能用基类
            //要加Show否则无法显示窗口
            MainWindow?.Show();

            //创建主程序并启动
            var MainWindow2 = App.Current.Services.GetService<MainWindow>();//泛型类必须为主窗口类，不能用基类
            //要加Show否则无法显示窗口
            MainWindow2?.Show();

            base.OnStartup(e);
        }

    }
}
